package com.woniuxy.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.woniuxy.service.IBooksService;
import com.woniuxy.entity.Books;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Lzj
 * @since 2023-09-16
 */
@Api(tags = "书籍管理")
@RestController
@RequestMapping("/books")
        public class BooksController {
    
    @Resource
    private IBooksService booksService;

    /**
     * 列表
     */
    @ApiOperation("查看所有书籍")
    @GetMapping
    public Result<List<Books>> findAll(){
        List<Books> list= booksService.list();
        return Result.ok("200",list);
    }

    /**
     * 分页列表
     * @param pageNum 当前页
     * @param pageSize 页大小
     */
    @ApiOperation("分页查询")
    @GetMapping("/page/{pageNum}/{pageSize}")
    public Result<Page<Books>>findPage(
    @PathVariable("pageNum")Integer pageNum,
    @PathVariable("pageSize")Integer pageSize){
        Page<Books> page= booksService.page(new Page<>(pageNum,pageSize));
        return Result.ok(page);
    }

    /**
     * 根据id查询
     */
    @ApiOperation("查询一个")
    @GetMapping("/{id}")
    public Result<Books> findOne(@PathVariable Integer id){
        return Result.ok(booksService.getById(id));
    }

    /**
     * 添加
     */
    @ApiOperation("添加")
    @PostMapping
    public Result saveOrUpdate(@RequestBody Books books){
        booksService.saveOrUpdate(books);
        return Result.ok("图书添加成功");
    }
    /**
     * 添加或修改
     */
    @ApiOperation("修改")
    @PostMapping("/update")
    public Result update(@RequestBody Books books){
        booksService.saveOrUpdate(books);
        return Result.ok("图书添加成功");
    }

    /**
     * 删除
     */
    @ApiOperation("删除一个")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        booksService.removeById(id);
        return Result.ok("图书删除成功");
    }

    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        booksService.removeByIds(ids);
        return Result.ok("批量删除成功");
    }


    }

